package com.lianxi.array;

import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        //数组转字符串
        int[] arr={1,2,3,4,5};
        String ret=Arrays.toString(arr);
        System.out.println(ret);



        //数组拷贝
        int[] arr2={1,2};
        arr2=Arrays.copyOf(arr,arr.length);
        System.out.println(Arrays.toString(arr2));
        //拷贝某一个范围
        arr2=Arrays.copyOfRange(arr,2,4);
        System.out.println(Arrays.toString(arr2));


        //二分查找
        int k=2;
        int left=0;
        int right=arr.length-1;
        while(left<right){
            int mid=(left+right)/2;
            if(mid==k){
                System.out.println("找到了下标为"+mid);
            }else if(k<mid){
                right=mid-1;
            }else{
                left=mid+1;
            }
        }


        //冒泡排序
        for (int i = 0; i < arr.length; i++) {
            int flg=0;
            for (int j = 1; j < arr.length-i-1; j++) {
                if(arr[j]>arr[j+1]){
                    int temp=arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=temp;
                    flg=1;
                }
            }
            if(flg==0){
                break;
            }
        }
        //内置方法对数组进行排序
        Arrays.sort(arr);





    }
    //数组逆序
    public void reverse(int[] arr){
        int left=0;
        int right=arr.length-1;
        while(left<=right){
            int temp=0;
            temp=arr[left];
            arr[left]=arr[right];
            arr[right]=temp;
            left++;
            right--;
        }
    }
}
